package com.it.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.reggie.Exception.BusinessException;
import com.it.reggie.annotation.Log;
import com.it.reggie.commen.BaseContext;
import com.it.reggie.commen.R;
import com.it.reggie.entity.Employee;
import com.it.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/employee")
@SuppressWarnings(value = "all")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //登录功能
    @PostMapping("login")
    public R login(@RequestBody Employee employee, HttpServletRequest request) {

        String username = employee.getUsername();

        //密码加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();

        qw.eq(username != null, Employee::getUsername, username);

        Employee one = employeeService.getOne(qw);


        if (one == null) {

            return R.error("用户名错误");

        }

        Long id = one.getId();

        String password1 = one.getPassword();

        Integer status = one.getStatus();


        if (password.equalsIgnoreCase(password1)) {

            //判断员工状态
            if (status == 0) {

                return R.error("员工已经禁用");

            } else {

                //成功登录，将id存储在session中
                request.getSession().setAttribute("id", id);

                return R.success(one);
            }

        } else {

            return R.error("密码错误");

        }
    }

    //退出登录清除session中存储的id
    @PostMapping("/logout")
    public R logOut(HttpServletRequest request) {

        request.getSession().removeAttribute("id");

        return R.success("成功退出");
    }

    //添加员工功能
    @PostMapping
    public R add(@RequestBody Employee employee, HttpServletRequest request) {

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        try {
            employeeService.save(employee);
        } catch (Exception e) {
            throw new BusinessException("用户名已存在");
        }

        return R.success("添加成功");
    }

    //员工分页查询功能
    @GetMapping("/page")
    public R selectByPage(String page, String pageSize, String name) {

        IPage<Employee> iPage = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));

        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();

        qw.like(StringUtils.isNotEmpty(name), Employee::getUsername, name);

        employeeService.page(iPage, qw);

        return R.success(iPage);


    }

    //修改员工状态
    @PutMapping
    public R updateStatus(@RequestBody Employee employee, HttpServletRequest request) {

        employeeService.updateById(employee);

        return R.success("修改成功");

    }

    @GetMapping("/{id}")
    public R upDateUser(@PathVariable Long id) {

        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();

        qw.eq(id != null, Employee::getId, id);

        Employee employeeServiceOne = employeeService.getOne(qw);

        return R.success(employeeServiceOne);

    }
}
